#include "SysOpenCV.h"
#include "BaiduApi/ocr.h"
#include <QDebug>
#include <QGraphicsScene>

SysOpenCV::SysOpenCV()
{

}

QString SysOpenCV::getIdNumber(QString PATH)
{
    // 设置APPID/AK/SK
    std::string app_id = "";
    std::string api_key = "";
    std::string secret_key = "";
    aip::Ocr client(app_id, api_key, secret_key);
    Json::Value result;
    std::string image;
    aip::get_file_content(PATH.toStdString().data(), &image);
    std::string id_card_side = "back";
    // 调用身份证识别
    result = client.idcard(image, id_card_side, aip::null);
    std::string idnum=result["words_result"]["公民身份号码"]["words"].toStyledString();
    QString Idata = QString::fromStdString(idnum);
    Idata = Idata.replace("\"","'");//QString字符串分割函数
    Idata = Idata.replace("\"","'");//QString字符串分割函数

    return Idata;
}

QImage  SysOpenCV::Mat2QImage(cv::Mat cvImg)
{
    QImage qImg;
    if(cvImg.channels()==3)                             //3 channels color image
    {

        cv::cvtColor(cvImg,cvImg,cv::COLOR_BGR2RGB);//default is BGR in opencv
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols, cvImg.rows,
//                    cvImg.cols*cvImg.channels(),//bytes per line(line stride)
                    cvImg.step,
                    QImage::Format_RGB888);
    }
    else if(cvImg.channels()==1)                    //grayscale image
    {
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols,cvImg.rows,
                    cvImg.cols*cvImg.channels(),
                    QImage::Format_Indexed8);
    }
    else
    {
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols,cvImg.rows,
                    cvImg.cols*cvImg.channels(),
                    QImage::Format_RGB888);// format reference
    }

    return qImg;

}
